/**
 * 微博js
 * version 1.0
 * author:chris
 */

//上传图片
$(".js-uploadPic").bind("click",function(){
	$("#pop_panel").remove();
	showPop($(this),"<form id=\"form-upload\" target=\"target-form\" action=\"?c=api&m=upload\" method=\"post\" enctype=\"multipart/form-data\">"+
					"<input id=\"file-input\" type=\"file\"  onchange=\"formSubmit('form-upload','正在上传...');\" name=\"pic\" style=\"display:block;\" />"+
					"<input id=\"file-upload-type\" type=\"hidden\"  name=\"type\" value=\"image\" style=\"display:none\" />"+
					"<input id=\"form-submit\" type=\"submit\" style=\"display:none\" />"+
					"</form>");
	$("#form-upload input:file").css({"border":"1px solid #e5e5e5","height":"25px"});
	
});

//表情
$(".js-brow").bind("click",function(){
	$("#pop_panel").remove();
	var brow_img = '';
	for(var i = 0;i< 60; i++){
		
		brow_img += "<img style='margin:2px' bname='"+i+"' src='/wb/images/brow/default/"+i+".gif' style='float:left' />";
	}
	showPop($(this),brow_img);
	$("#pop_panel img").each(function(){
		$(this).bind("click",function(){
			var bname = $(this).attr("bname");
			//$(".blogarea-textarea textarea").val("[:"+bname+"]");
			insertText(document.getElementById("blog_input"),"[:"+bname+"]");
			$("#pop_panel").remove();
		});
	});
});

//表单提交
function formSubmit(id,msg)
{
	
	$("#"+id).submit();
	$("#pop_panel .pop_content").html(msg);
}

//弹层
function showPop(obj,data)
{
	//var obj = $(".uploadPic");
	var top = obj.offset().top+20+"px";
	var left = obj.offset().left+"px";
	var width = "300px";
	var height = "100px";
	pop_panel(data,left,top,"300px","100px");
}

function removePanel()
{
	$("#form-upload").remove();
	setTimeout(function(){$("#pop_panel").remove()},3000);
}

//textarea插入数据
function insertText(obj,str) {
	obj.focus();
	if (document.selection) {
		var sel = document.selection.createRange();
		sel.text = str;
	} else if (typeof obj.selectionStart == 'number' && typeof obj.selectionEnd == 'number') {
		var startPos = obj.selectionStart,
			endPos = obj.selectionEnd,
			cursorPos = startPos,
			tmpStr = obj.value;
		obj.value = tmpStr.substring(0, startPos) + str + tmpStr.substring(endPos, tmpStr.length);
		cursorPos += str.length;
		obj.selectionStart = obj.selectionEnd = cursorPos;
	} else {
		obj.value += str;
	}
}

//字数统计
$(".blogarea-textarea textarea").bind("keydown keyup blur focus change",function(){
	var blogcontent = $(this).val().replace(/(^\s*)|(\s*$)/g, "").replace(/\n|\r/g,"");
	var blength = blogcontent.length;
	var chartotal = 140 - blength;
	if(chartotal <= 0){
		chartotal = 0;
		blogcontent = blogcontent.substring(0,140);
		$(this).val(blogcontent);
		$(".char-constantia").html("<font color='red'>"+chartotal+"</font>");
	}else{
		$(".char-constantia").html(chartotal);
	}
	$("#pop_panel").remove();
});

function zoom(){
	$("#blogpic img").each(function(){
		$(this).parent().focus(function(){
			$(this).blur();
		});
		
		$(this).toggle(function(){
			$(this).css({"width":"90%","height":"90%"});
		},function(){
			$(this).css({"width":"100px","height":"100px"});
		});
	});
}

zoom();

//发微博
$("#js-send").bind("click",function(){
	var content = $(".blogarea-textarea textarea").val();
	var image = $("#upload-picurl").val();
	if(content == ""){
		alert("请输入你想说的话！");
		$(".blogarea-textarea textarea").focus();
	}else{
		$.post("?c=api&m=write",{content:content,image:image},function(data){
			var data = eval(data);
			if(typeof(data) == "object"){
				var new_html = "<li>"+
									"<div class=\"bloguser-avatar\"><a href=\"?c=user&uid="+data.wb_uid+"\"><img title=\""+data.userinfo['wb_nickname']+"\" src=\""+data.userinfo['wb_avatar']+"\"></a></div>"+
									"<div class=\"blogbody\">"+
										"<div class=\"bloguser-username\">"+
											"<a href=\"?c=user&uid="+data.wb_uid+"\">"+data.userinfo['wb_nickname']+"</a>："+
										"</div>"+
										"<div class=\"blogcontent\">"+
											data.wb_content+
										"</div>"+
										"<div class=\"bloglist-foot\">"+
				    						"<div class=\"bloglist-from\"><a href=\"?detail&id="+data.wb_id+"\">"+data.wb_ptime+"</a>来自"+data.wb_from+" </div>"+
				    						"<div class=\"bloglist-tool\">"+
				    						"<a href=\"javascript:void(0)\" id=\"js-forward\">转发 </a>"+
				    						"| "+
				    						"<a href=\"javascript:void(0)\" id=\"js-comments\">评论</a>"+
				    						
				    						"</div>"+
			    						"</div>"+
									"</div>"+
								"</li>";
				$(".bloglist-list li:eq(0)").before(new_html);
				$(".blogarea-textarea textarea").val("");
				$("#upload-picurl").val('');
				$(".js-wordCounter .char-constantia").text("140");
				zoom();
			}else{
				alert("出错了！");
			}
			
		},"json");
	}

});


$(".bloglist-tool a").each(function(){
	
	//转发微博
	if($(this).attr("id") == "js-forward"){
		var forward_data = eval($(this).attr("data"));
		$(this).bind("click",function(){
			$(this).parent().parent().parent().find(".blogcomments").remove();
			var status = $(this).parent().parent().parent().find(".blogforward").html();
			if(status == null){
				$(this).parent().after("<div class=\"blogforward\">"+
								"<span class=\"icon-up\"></span>"+
								"<a title=\"关闭\" class=\"close-icon\" href=\"javascript:void(0)\" onfocus=\"this.blur()\">x</a>"+
								"<div class=\"blogforward-main\">"+
									"<div class=\"blogforward-body\">"+
										"<h3 class=\"blogforward-title\">转发微博</h3>"+
										"<textarea class=\"blogforward-textarea\"></textarea>"+
										"<div class=\"blogforward-foot\">"+
											"<a href=\"javascript:;\" class=\"forward-icon\"><em>转 发</em></a>"+
										"</div>"+
									"</div>"+
								"</div>"+
						"</div>"
						);
				var forward = $(this).parent().parent().parent().find(".blogforward");
				var textarea = forward.find("textarea");
				if(forward_data[1] > 0){
					textarea.val("//@"+forward_data[2]+" ");
					textarea.focus();
					textarea.val(textarea.val());
				}else{
					textarea.focus();
				}
				forward.find(".close-icon").bind("click",function(){
					$(this).parent().remove();
				});
				forward.find(".forward-icon").bind("click",function(){
					var fid = forward_data[1] == 0 ? forward_data[0] : forward_data[1];
					if(parseInt(uid) > 0){
						var content = textarea.val() == "//@"+forward_data[2]+" " ? "" : textarea.val();
						$.post("?c=api&m=write",{content:content,type:"forwards",id:forward_data[0],fid:fid},function(data){
							//alert(data);
							forward.parent().find("#forwards-total").text(parseInt(forward.parent().find("#forwards-total").text()) + 1);
							textarea.parent().parent().parent().html("<h1 style='text-align:center;line-height:35px;font-size:14px;font-weight:400'>转发成功！</h1>");
							setTimeout(function(){forward.remove()},1000);
							
						},"json");
					}
						
				});
			}
		});
	}

	if($(this).attr("id") == "js-comments"){
		var comments_data = eval($(this).attr("data"));
		$(this).bind("click",function(){
			$(this).parent().parent().parent().find(".blogforward").remove();
			var status = $(this).parent().parent().parent().find(".blogcomments").html();
			if(status == null){
				$(this).parent().after("<div class=\"blogcomments\">"+
								"<span class=\"icon-up\"></span>"+
								"<a title=\"关闭\" class=\"close-icon\" href=\"javascript:void(0)\" onfocus=\"this.blur()\">x</a>"+
								"<div class=\"blogcomments-main\">"+
									"<div class=\"blogcomments-body\">"+
										"<h3 class=\"blogcomments-title\">评论微博</h3>"+
										"<textarea class=\"blogcomments-textarea\"></textarea>"+
										"<div class=\"blogcomments-foot\">"+
										"<span style='float:left'><input type='checkbox' />同时转发到我的微博</span>"+
											"<a href=\"javascript:;\" class=\"comments-icon\"><em>评 论</em></a>"+
										"</div>"+
										"<ul>"+
										"</ul>"+
									"</div>"+
								"</div>"+
						"</div>"
						);
				var comments = $(this).parent().parent().parent().find(".blogcomments");
				var textarea = comments.find("textarea");
				textarea.focus();
				comments.find(".close-icon").bind("click",function(){
					$(this).parent().remove();
				});

				//获取评论
				if(parseInt(comments.parent().find("#comments-total").text()) > 0){

					comments.find("ul").append("<li style='text-align:center;border:0px;height:30px;'><img src='images/common/loading.gif' /></li>");
					
					var cid = comments_data[2] == 0 ? comments_data[0] : comments_data[2];
					$.post("?c=api&m=getcomments",{cid:cid},function(data){
						var cresult = eval(data);
						var clist = "";
						for(var i = 0;i < cresult.length;i++){
							clist += "<li><a href='?c=user&uid="+cresult[i].wb_uid+"'>"+cresult[i].userinfo.wb_nickname+"</a>："+cresult[i].wb_content+"<span style='margin-left:20px;color:#cccccc'>"+cresult[i].wb_ptime+"</span>&nbsp;&nbsp;-&nbsp;&nbsp;<span id='reply'><a href='javascript:void(0)' nickname='"+cresult[i].userinfo.wb_nickname+"'><font color='#8BA4CD'>回复</font></a></span></li>";
						}
						comments.find("ul").html(clist);

						if(cresult.length >= 10){
							comments.find("ul").append("<li><span style='float:right;margin-right:5px;'><a href='?c=user&cid="+cid+"'>查看更多评论...</a></span></li>");
						}

						comments.find("ul li:last").css("border","0px");
						
						comments.find("ul li").each(function(){
							$(this).find("#reply a").bind("click",function(){
								textarea.val("回复@"+$(this).attr("nickname")+" ："+textarea.val());
								textarea.focus();
							});
						});
					});
				}

				
				//提交评论内容
				comments.find(".comments-icon").bind("click",function(){
					var cid = comments_data[2] == 0 ? comments_data[0] : comments_data[2];
					var fid = comments_data[1] == 0 ? comments_data[0] : comments_data[1];
					var isForward = comments.find("input:checkbox:checked").val() == "on" ? 1 : 0;

					
					
					if(parseInt(uid) > 0){
						var content = textarea.val();
						if(content == ''){
							alert("输入评论内容！");
						}else{
							$.post("?c=api&m=write",{content:content,type:"comments",id:comments_data[0],isForward:isForward,fid:fid,nickname:comments_data[3],cid:cid},function(data){
								//alert(data);
								comments.parent().find("#comments-total").text(parseInt(comments.parent().find("#comments-total").text()) + 1);
								textarea.parent().parent().parent().html("<h1 style='text-align:center;line-height:35px;font-size:14px;font-weight:400'>评论成功！</h1>");
								setTimeout(function(){comments.remove()},1000);
								
							},"json");
						}
					}
						
				});
			}
		});
	}

	
});
